package com.ruoyi.crop.controller;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.crop.api.domain.MaterialLot;
import com.ruoyi.crop.api.domain.SrcPage;
import com.ruoyi.crop.service.MaterialLotService;
import com.ruoyi.crop.service.PageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @author : Guosir
 * @date : 2024-07-28 21:09
 * @description :
 **/
@RestController
@RequestMapping("page")
public class PageController extends BaseController {
    @Autowired
    private PageService pageService;


    @Autowired
    private MaterialLotService materialLotService;

    /**
     * 查询包装列表
     */
    @RequiresPermissions("crop:page:list")
    @GetMapping("/list")
    public TableDataInfo list(SrcPage srcPage)
    {
        startPage();
        List<SrcPage> list = pageService.listAll(srcPage);
        return getDataTable(list);
    }

    /**
     * 获取包装详细信息
     */
    @RequiresPermissions("crop:page:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pageService.getById(id));
    }

    /**
     * 新增包装
     */
    @RequiresPermissions("crop:page:add")
    @Log(title = "包装", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SrcPage srcPage)
    {
        srcPage.setPageCode("BX"+ IdUtil.getSnowflakeNextIdStr());
        return toAjax(pageService.save(srcPage));
    }

    /**
     * 修改包装
     */
    @RequiresPermissions("crop:page:edit")
    @Log(title = "包装", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SrcPage srcPage)
    {
        return toAjax(pageService.updateById(srcPage));
    }

    /**
     * 删除包装
     */
    @RequiresPermissions("crop:page:remove")
    @Log(title = "包装", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pageService.removeBatchByIds(Arrays.asList(ids)));
    }

    // /**
    //     * 导出包装列表
    //     */
    //    @RequiresPermissions("crop:page:export")
    //    @Log(title = "包装", businessType = BusinessType.EXPORT)
    //    @PostMapping("/export")
    //    public void export(HttpServletResponse response, SrcPage srcPage)
    //    {
    //        List<SrcPage> list = srcPageService.selectSrcPageList(srcPage);
    //        ExcelUtil<SrcPage> util = new ExcelUtil<SrcPage>(SrcPage.class);
    //        util.exportExcel(response, list, "包装数据");
    //    }


    /**
     * 获取批次号
     */
    @GetMapping("materialLoglist")
    public AjaxResult materialLoglist(){
        List<MaterialLot> list = pageService.listmaterList();
        return success(list);
    }
}
